package com.soundslikeobiwan.ontocalendar;

import java.lang.*;
import java.util.*;
import java.util.Date.*;

import javax.microedition.pim.*;

import net.rim.blackberry.api.menuitem.*;
import net.rim.blackberry.api.mail.*;
// import net.rim.blackberry.api.pim.*;

//import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.blackberry.api.invoke.*;

/*
 * OntoCalendar application
 * This registers a simple menu item with the system to be shown
 * when viewing an email Message. The run method for this menuItem
 * simply gets the message for use
 *
 * Copyright (C) 2003 Research In Motion Ltd.
 */

class OntoCalendar  {

    public static void main(String[] args){
        System.out.println("Starting MyMenuItemDemo");
       (new MyMenuItem(0)).registerInstance();
        OptionsManager.Register();

    }
}

class MyMenuItem extends ApplicationMenuItem
{

    // we'll use the default constructors here.
    MyMenuItem(int order){
        super(order);
    }

    // Register the instance of the menuItem with the system
    public void registerInstance(){
        System.out.println("Registering MyMenuItemDemo");
        ApplicationMenuItemRepository.getInstance().removeMenuItem(ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW,this);
        ApplicationMenuItemRepository.getInstance().addMenuItem(ApplicationMenuItemRepository.MENUITEM_EMAIL_VIEW,this);
        //Dialog.inform("OntoCalendar v 1.1 ready");
    }
   
   // from: http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800698/How_To_-_Retrieve_More_of_a_Message.html?nodeid=800776&vernum=0
   //Returns a String obtained from the contents of all BodyParts
    //of a Multipart message.
    private String getBodyContent(BodyPart bp)
    {
        //Get the parent Multipart of the BodyPart of this message.
        Multipart mp = bp.getParent();
        StringBuffer sb = new StringBuffer();

        //Loop through all child BodyParts of the Multipart.
        for (int mpCount = 0; mpCount < mp.getCount(); ++mpCount)
        {
            //Assuming the message is text based TextBodyPart can be used.
            TextBodyPart tp = (TextBodyPart)mp.getBodyPart(mpCount);
            //Append the content of each BodayPart to a StringBuffer.
            sb.append((String)tp.getContent());
        }
        return sb.toString();
    }


    //methods we must implement
    //Run is called when the menuItem is invokes
    public Object run(Object context) {

        if (context instanceof Message) {
            try {
                Message message = (Message) context;
                String content;
                
                EventList el = (EventList)PIM.getInstance(). openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE);
                Event newEvent = el.createEvent();
               
                content = message.getSubject();
                if (null != content) {
                    newEvent.addString(Event.SUMMARY, Event.ATTR_NONE, content);
                }
               
                Object obj = message.getContent();
                if (obj instanceof Multipart) {
                    Multipart mp=(Multipart)obj;
                    StringBuffer sb = new StringBuffer();
                    System.out.println(mp.getContentType());
                                
                    //Loop through all child BodyParts of the Multipart.
                    for (int mpCount = 0; mpCount < mp.getCount(); ++mpCount) {
                        BodyPart bp = mp.getBodyPart(mpCount);
                        if (bp instanceof TextBodyPart) {

                            TextBodyPart tp = (TextBodyPart)bp;
                            
                            //Append the content of each BodyPart to a StringBuffer.
                            sb.append((String)tp.getContent());
                        }
                    }
 
                    if (null != sb) {
                        newEvent.addString(Event.NOTE, Event.ATTR_NONE, sb.toString());
                    }
                } else if (obj instanceof SupportedAttachmentPart) {
                    SupportedAttachmentPart ap = (SupportedAttachmentPart)obj;

                    System.out.println(ap.getContentType());
                       
                } else if (obj instanceof UnsupportedAttachmentPart) {
                    UnsupportedAttachmentPart ap = (UnsupportedAttachmentPart)obj;

                    System.out.println(ap.getContentType());

                } else if (obj instanceof BodyPart) {

                    BodyPart bp = (BodyPart)obj;

                    System.out.println(bp.getContentType());
                    // multipart/alternative?
                    System.out.println(message.getBodyText());

/*                    
                    content = (String)bp.getContent();
                    if (content != null)
                       newEvent.addString(Event.NOTE, Event.ATTR_NONE, message.getBodyText());                 
*/                                              
                } else {
                    System.out.println(message.getContentType());
                    content = message.getBodyText();
                    if (content != null)
                        newEvent.addString(Event.NOTE, Event.ATTR_NONE, message.getBodyText());
                }
                    

                newEvent.addDate(Event.START, Event.ATTR_NONE, System.currentTimeMillis());
                newEvent.addDate(Event.END, Event.ATTR_NONE, 60000+System.currentTimeMillis());
                
                Invoke.invokeApplication(Invoke.APP_TYPE_CALENDAR, 
                    new CalendarArguments(CalendarArguments.ARG_NEW, newEvent) );
            } catch (Exception ex) {
                Dialog.alert(ex.getClass().getName() + ": " + ex.getMessage());
            }
        }

        return context;
    }

    //toString should return the string we want to
    //use as the lable of the menuItem
    public String toString(){
        return "Onto Calendar...";
    }

}

